package com.jmu.edu.IMApplication.utils;

/**
 * @Author: Stars
 */
import android.app.Activity;
import android.content.ContentResolver;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SystemUtil {
    /**
     * 获取图片的路径
     *
     * @param uri
     *  图片的URI
     * @return
     */
    public static String getPath(Activity context, Uri uri) {
        System.out.println( uri.getLastPathSegment ());
        ContentResolver cr = context.getContentResolver();
        System.out.println(uri.toString());
        System.out.println(uri.getPath());
        Cursor cursor = cr.query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            System.out.println(i+"-----------------"+cursor.getString(i));
        }
        System.out.println();
        return cursor.getString(2);
    }

    public static String getJson(String fileName, Activity activity) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = activity.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}